<?php
//------------------------
// 前台首页控制器
//-------------------------

namespace app\home\controller;

use app\common\controller\HomeBase;
use think\Session;
use app\common\model\Goods as GoodsModel;
use think\Cookie;


class Index extends HomeBase
{
	protected $GoodsModel;

	protected function _initialize()
    {
        parent::_initialize();
        $this->GoodsModel              = new GoodsModel();
    }



    public function index(){
    	// halt(Session::get(''));
    	// 特价促销品
    	$promoteGoods = $this->GoodsModel ->getPromoteGoods();
    	$this->assign('promoteGoods',$promoteGoods);

    	// 新品
    	$newGoods = $this->GoodsModel ->getNew();
    	$this->assign('newGoods',$newGoods);


    	// 推荐品
    	$bestGoods = $this->GoodsModel ->getBest();
    	$this->assign('bestGoods',$bestGoods);

    	$this->assign('megamenuOpen',1);//展开分类菜单
        return $this->fetch();
    }

     // 取出最近浏览过的商品的信息
    public function ajaxGetRecentDisGoods(){
    	// 先从COOKIE中取出最近浏览过的商品的ID
    	$recentDisplay = Cookie::has('recentDisplay') ? unserialize(Cookie::get('recentDisplay')) : array();

    	if($recentDisplay){
    		// 再根据商品的ID取出商品的信息
    		$recentDisplay_str = implode(',', $recentDisplay);
    		$goods = $this->GoodsModel->field('id,goods_name,goods_image')->where('id','in',$recentDisplay_str)->order("INSTR(',$recentDisplay_str,',CONCAT(',',id,','))")->select()->toArray();

    		halt($goods);
    		echo json_encode($goods);
    	}
    }
}
